package com.shiliao.app.activity.index.presenter;

import android.text.TextUtils;

import com.shiliao.app.activity.index.service.INewsDetailService;
import com.shiliao.app.activity.index.service.impl.NewsDetailImpl;
import com.shiliao.app.activity.index.view.NewsDetailView;
import com.shiliao.app.utils.YMServerUtil;
import com.yimei.framework.mvp.presenter.IBasePresenter;
import com.yimei.framework.network.ICallBack;
import com.yimei.framework.util.LogUtil;
import com.yimei.framework.util.ParseHelper;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yinzhiqiang on 2017/10/9.
 */

public class NewsDetailPresenter extends IBasePresenter<INewsDetailService> {
    private NewsDetailView newsDetailView;
    private INewsDetailService iNewsDetailService;

    public NewsDetailPresenter(NewsDetailView newsDetailView) {
        this.newsDetailView = newsDetailView;
        iNewsDetailService = new NewsDetailImpl();
        setService(iNewsDetailService);
    }

    public void requestComment(String articleId, String userId, String Comment) {//请求评论接口
        Map map = new HashMap();
        if (!TextUtils.isEmpty(articleId)) {
            map.put("article_id", articleId);
        }
        if (!TextUtils.isEmpty(userId)) {
            map.put("user_id", userId);
        }
        if (!TextUtils.isEmpty(Comment)) {
            map.put("comment", Comment);
        }

        iNewsDetailService.requestComment(YMServerUtil.ACTION_COMMENT, map, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                newsDetailView.loadPage();
                newsDetailView.commentSuccess();
            }

            @Override
            public void onFail(int statusCode, Object result) {

            }
        });
    }

    public void requestCollection(String articleId, String userId) {//请求收藏接口
        Map map = new HashMap();
        if (!TextUtils.isEmpty(articleId)) {
            map.put("article_id", articleId);
        }
        if (!TextUtils.isEmpty(userId)) {
            map.put("user_id", userId);
        }
        iNewsDetailService.requestComment(YMServerUtil.ACTION_COLLECTION, map, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                newsDetailView.setCollectionIcon();
            }

            @Override
            public void onFail(int statusCode, Object result) {

            }
        });
    }

    public void requestShare(String articleId, String userId) {//请求分享内容
        Map map = new HashMap();
        if (!TextUtils.isEmpty(articleId)) {
            map.put("article_id", articleId);
        }
        if (!TextUtils.isEmpty(userId)) {
            map.put("user_id", userId);
        }
        iNewsDetailService.requestShare(YMServerUtil.ACTION_SHARE, map, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                LogUtil.e("yzq", result.toString());
                String sharTitle = ParseHelper.getString(result.toString(), "title");
                String sharDescription = ParseHelper.getString(result.toString(), "description");
                String sharPicurl = ParseHelper.getString(result.toString(), "pic_url");
                String sharurl = ParseHelper.getString(result.toString(), "url");
                newsDetailView.shareContent(sharTitle, sharPicurl, sharurl, sharDescription);
            }

            @Override
            public void onFail(int statusCode, Object result) {

            }
        });
    }

    public void requestCancelArticle(String articleId, String userId) {//取消收藏
        Map map = new HashMap();
        if (!TextUtils.isEmpty(articleId)) {
            map.put("article_id", articleId);
        }
        if (!TextUtils.isEmpty(userId)) {
            map.put("user_id", userId);
        }
        iNewsDetailService.requestCancelArticle(YMServerUtil.ACTION_CANCEL_ARTICLE, map, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                newsDetailView.setNoCollection();
            }

            @Override
            public void onFail(int statusCode, Object result) {

            }
        });

    }

    public void requestIsCollection(String articleId, String userId) {//该用户是否收藏该文章接口
        Map map = new HashMap();
        if (!TextUtils.isEmpty(articleId)) {
            map.put("article_id", articleId);
        }
        if (!TextUtils.isEmpty(userId)) {
            map.put("user_id", userId);
        }
        iNewsDetailService.requestIsCollection(YMServerUtil.ACTION_IS_COLLECTION, map, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                String isCollection = ParseHelper.getString(result.toString(), "stored");
                String isScored = ParseHelper.getString(result.toString(), "scored");
                newsDetailView.isCollection(isCollection, isScored);
                LogUtil.e("yzq", isScored + ".." + isScored + "......" + result.toString());
            }

            @Override
            public void onFail(int statusCode, Object result) {

            }
        });
    }

    public void requestEvaluate(String articleId, String userId, String scoring) {
        Map map = new HashMap();
        if (!TextUtils.isEmpty(articleId)) {
            map.put("article_id", articleId);
        }
        if (!TextUtils.isEmpty(userId)) {
            map.put("user_id", userId);
        }
        if (!TextUtils.isEmpty(scoring)) {
            map.put("scoring", scoring);
        }
        iNewsDetailService.requestEvaluate(YMServerUtil.ACTION_ARTICLE_EVALUATE, map, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                LogUtil.e("yzq", "................");
            }

            @Override
            public void onFail(int statusCode, Object result) {

            }
        });
    }

}
